package com.yanxinim.cn.store;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.yanxinim.cn.AppConfig;
import com.yanxinim.cn.R;
import com.yanxinim.cn.store.adapter.StoreAddressListAdapter;
import com.yanxinim.cn.store.bean.StoreAddressInfo;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.util.JsonTool;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class StoreUserAddressActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_add_address)
    ImageView ivAddAddress;
    @BindView(R.id.rv_address_list)
    RecyclerView rvAddressList;

    int type;
    @BindView(R.id.iv_nothing)
    ImageView ivNothing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_store_user_address_activity);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        rvAddressList.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("userId", getUser().getUserId());
        map.put("pageIndex", "0");
        map.put("pageSize", "100");
        HttpUtils.get().url(AppConfig.STORE_GET_ADDRESS)
                .params(map)
                .build().execute(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                JSONObject jsonObject = JSON.parseObject(result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (jsonObject.getInteger("resultCode") == 1) {
                            JSONObject jsonObject1 = JSON.parseObject(jsonObject.getString("data"));
                            List<StoreAddressInfo> storeAddressInfos = JsonTool.jsonToList(jsonObject1.getString("pageData"), StoreAddressInfo.class);
                            if (storeAddressInfos != null && storeAddressInfos.size()>0){
                                StoreAddressListAdapter storeAddressListAdapter = new StoreAddressListAdapter(StoreUserAddressActivity.this, storeAddressInfos);
                                storeAddressListAdapter.setItemOnClick(new StoreAddressListAdapter.ItemOnClick() {
                                    @Override
                                    public void itemOnClick(StoreAddressInfo storeAddressInfo) {
                                        if (type == 2) {
                                            Intent intent = getIntent();
                                            intent.putExtra("address", storeAddressInfo);
                                            setResult(2021, intent);
                                            finish();
                                        } else {
                                            Intent intent = new Intent(StoreUserAddressActivity.this, StoreCreateAddressActivity.class);
                                            intent.putExtra("type", 2);
                                            intent.putExtra("addressInfo", storeAddressInfo);
                                            startActivity(intent);
                                        }

                                    }
                                });
                                rvAddressList.setAdapter(storeAddressListAdapter);
                                ivNothing.setVisibility(View.GONE);
                                rvAddressList.setVisibility(View.VISIBLE);
                            }else {
                                ivNothing.setVisibility(View.VISIBLE);
                                rvAddressList.setVisibility(View.GONE);
                            }

                        }else {
                            ivNothing.setVisibility(View.VISIBLE);
                            rvAddressList.setVisibility(View.GONE);
                        }
                        Log.i("qin_store", result);
                    }
                });

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.iv_add_address})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_add_address:
                startActivity(new Intent(StoreUserAddressActivity.this, StoreCreateAddressActivity.class));
                break;
        }
    }
}
